package com.lxk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.pojo.*;
import com.lxk.service.RechargeOrderService;
import com.lxk.service.RechargeService;
import com.lxk.service.UsersService;
import com.lxk.util.ImgUrlUtil;
import com.lxk.util.TermUtil;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充值信息 前端控制器
 * </p>
 *
 * @author JohnSoybean
 * @since 2020-07-14
 */
@RestController
@RequestMapping("/recharge")
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private UsersService usersService;

    /**
     * 获取充值套餐列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<Recharge>> getRechargeList() {
        List<Recharge> list = rechargeService.list();
        return Result.success(list);
    }

    /**
     * 获取银币余额
     * @return
     */
    @GetMapping("/coins")
    public Result<Integer> getCoins(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.select("wallet");
        wrapper.eq("user_id", userId);
        Users users = usersService.getOne(wrapper);
        if (users == null) {
            return Result.error(CodeMsg.USER_NOT_EXIST);
        }
        return Result.success(users.getWallet());
    }

    /**
     * 历史充值记录
     *
     * @return
     */
    @GetMapping("/orders")
    public Result<PageModel<RechargeOrder>> showRechargeOrderList(Integer pageNum, Integer pageSize) {
        Page<RechargeOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<RechargeOrder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("recharge_order_id");
        rechargeOrderService.page(page, wrapper);
        PageModel<RechargeOrder> model = new PageModel<>(page);
        return Result.success(model);
    }


    /**
     * 生成充值订单 - 套餐
     * @param request
     * @param rechargeId
     * @return
     */
    @PostMapping("/orders/init/pack")
    public Result<Long> initRechargeOrderPack(HttpServletRequest request, Long rechargeId) {
        Long userId = (Long) request.getAttribute("userId");
        Recharge recharge = rechargeService.getById(rechargeId);
        if (recharge == null) {
            return Result.error(CodeMsg.RECHARGE_NOT_EXIST);
        }
        //生成订单
        RechargeOrder order = new RechargeOrder();
        order.setRechargeId(rechargeId);
        order.setRechargePrice(recharge.getRechargePrice());
        order.setRechargeTotal(recharge.getRechargeTotal());
        order.setUserId(userId);
        boolean save = rechargeOrderService.save(order);
        if (save) {
            return Result.success(order.getRechargeOrderId());
        } else {
            return Result.error(CodeMsg.ORDER_INIT_ERR);
        }
    }

    /**
     * 生成充值订单 - 其他
     * @param request
     * @param price
     * @return
     */
    @PostMapping("/orders/init/other")
    public Result<Long> initRechargeOrderOther(HttpServletRequest request, Integer price) {
        Long userId = (Long) request.getAttribute("userId");
        if (null == price || price <= 0) {
            return Result.error(CodeMsg.RECHARGE_INVALID);
        }
        if (price % 100 != 0) {
            return Result.error(CodeMsg.RECHARGE_INVALID);
        }
        Integer coin = price / 100;
        //生成订单
        RechargeOrder order = new RechargeOrder();
        order.setRechargeId(0L);
        order.setRechargePrice(price);
        order.setRechargeTotal(coin);
        order.setUserId(userId);
        boolean save = rechargeOrderService.save(order);
        if (save) {
            return Result.success(order.getRechargeOrderId());
        } else {
            return Result.error(CodeMsg.ORDER_INIT_ERR);
        }
    }


}

